package com.daz.manager.studentScore.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.daz.common.Util.CommonUtil;
import com.daz.manager.studentScore.pojo.StudentScoresPojo;
import com.daz.manager.studentScore.server.IStudentScoresSV;
import com.daz.student.information.pojo.StudentInfoPojo;
import com.daz.student.summary.pojo.SummaryPojo;
import com.github.pagehelper.PageInfo;
import com.opensymphony.xwork2.ActionSupport;

public class StudentScoreAction extends ActionSupport{
	private String id;
	private String ids;
	private String studentId;
	private String StudentName;
	private String courseName;
	private String credit;
	private String testScores;
	private String peacetimeScores;
	private String prom;
	private String grade;
	private String hourse;
	private String isAgain;
	private String scores;
	
	
	private Boolean success;
	private String msg;
	private File file; 
	private String uploadContentType;
	public String page;
	public String limit;
	public String query;
	private String studentScoreListStr;
	private PageInfo<StudentScoresPojo> resultList;
	@Autowired
	private IStudentScoresSV sv;
	
	public String searchStudentScores(){
		Map<String, Object> map = new HashMap<String, Object>();
		int s=0;
		int l=0;
		if(page!=null){
			s = Integer.parseInt(page);
		}
		if(limit != null){
			l = Integer.parseInt(limit);
		}
		if(studentId != null && !"".equals(studentId)){
			map.put("studentId", studentId);
		}
		if(StudentName != null && !"".equals(StudentName)){
			map.put("studentName", StudentName);
		}
		if(courseName != null && !"".equals(courseName)){
			map.put("courseName", courseName);
		}
		if(grade != null && !"".equals(grade)){
			map.put("grade", grade);
		}
		if(prom != null && !"".equals(prom)){
			map.put("prom", prom);
		}
		
		if(CommonUtil.isNotBlank(query)){
			map.put("query",query);
		}
		try {
			resultList = sv.searchStudentScores(map, s, l);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String insertSelective(){
	
		int inputNum = 0;
		int successNum = 0;
		try {
			if(file.isFile()){
	        	InputStream input = null;
	            try {
	            	input = new FileInputStream(file);
	            	XSSFWorkbook workBook;
	            	workBook = new XSSFWorkbook(input);
					XSSFSheet sheet = workBook.getSheetAt(0);  
		            if (sheet != null)  
		            { 	inputNum = sheet.getPhysicalNumberOfRows()-1;
		            	//学号，姓名，课程名称，学分，平时成绩，考试成绩，学年，年级，学时，是否补考，分数
		                for (int i = 1; i < sheet.getPhysicalNumberOfRows();i++)  
		                {  
		                	XSSFRow row = sheet.getRow(i);
		                	StudentScoresPojo value = new StudentScoresPojo();
		                    if(row.getCell(1)!= null && !"".equals(row.getCell(0).toString().trim())){
		                    	String num = row.getCell(0).toString().trim();
		                    	long n = Long.parseLong(num);
		                    	value.setStudentId(String.valueOf(n));
		                    }else{
		                    	break;
		                    }
		                    if(row.getCell(1)!= null && !"".equals(row.getCell(1).toString().trim())){
		                    	value.setStudentName(row.getCell(1).toString().trim());
		                    }else{
		                    	break;
		                    }
		                    if(row.getCell(2)!= null && !"".equals(row.getCell(2).toString().trim())){
		                    	value.setCourseName(row.getCell(2).toString().trim());
		                    }else{
		                    	break;
		                    }
		                    if(row.getCell(3)!= null && !"".equals(row.getCell(3).toString().trim())){
		                    	String num = row.getCell(3).toString().trim();
		                    	long n = Long.parseLong(num);
		                    	value.setCredit(String.valueOf(n));
		                    }else{
		                    	break;
		                    }
		                    if(row.getCell(4)!= null && !"".equals(row.getCell(4).toString().trim())){
		                    	String num = row.getCell(4).toString().trim();
		                    	long n = Long.parseLong(num);
		                    	value.setPeacetimeScores(String.valueOf(n));
		                    }else{
		                    	break;
		                    }
		                    if(row.getCell(5)!= null && !"".equals(row.getCell(5).toString().trim())){
		                    	String num = row.getCell(5).toString().trim();
		                    	long n = Long.parseLong(num);
		                    	value.setTestScores(String.valueOf(n));
		                    }else{
		                    	break;
		                    }
		                    if(row.getCell(6)!= null && !"".equals(row.getCell(6).toString().trim())){
		                    	
		                    	value.setProm(row.getCell(6).toString().trim());
		                    }else{
		                    	break;
		                    }
		                    if(row.getCell(7)!= null && !"".equals(row.getCell(7).toString().trim())){
		                    
		                    	String num = row.getCell(7).toString().trim();
		                    	long n = Long.parseLong(num);
		                    	value.setGrade(String.valueOf(n));
		                    }else{
		                    	break;
		                    }
		                    if(row.getCell(8)!= null && !"".equals(row.getCell(8).toString().trim())){
		                    	String num = row.getCell(8).toString().trim();
		                    	long n = Long.parseLong(num);
		                    	value.setHourse(String.valueOf(n));
		                    }else{
		                    	break;
		                    }
		                    if(row.getCell(9)!= null && !"".equals(row.getCell(9).toString().trim())){
		                    	if("是".equals(row.getCell(9).toString().trim())){
		                    		value.setIsAgain("1");
		                    	}else{
		                    		value.setIsAgain("0");
		                    	}
		                    }else{
		                    	value.setIsAgain("0");
		                    }
		                    if(row.getCell(10)!= null && !"".equals(row.getCell(10).toString().trim())){
		                    	String num = row.getCell(10).toString().trim();
		                    	long n = Long.parseLong(num);
		                    	value.setScores(String.valueOf(n));
		                    }else{
		                    	break;
		                    }
		                    sv.insertSelective(value);
		                    successNum++;
							}
		                //teacherServer.addTeacherBatch(teachersList);
		                this.setSuccess(true);
		                this.setMsg("导入"+inputNum+"条数据，成功"+successNum+"条，失败"+(inputNum-successNum)+"条");
		                }
	    		} catch (IOException e) {
	    			e.printStackTrace();
	    			this.setSuccess(false);
	    			this.setMsg("文件解析失败！");
	    		}
	        }else{
	        this.setSuccess(false);
	        this.setMsg("文件解析失败！");
	        }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.setSuccess(false);
			this.setMsg("文件解析失败！");
		}
		return SUCCESS;
	}
	
	public String deleteStudentScore(){
		String sId[] = ids.split(",");
		try { 
			for(int i=0; i < sId.length; i++){
				HashMap map = new HashMap();
				map.put("id", sId[i]);
				sv.deleteStudentScore(map);
			}
			this.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.setSuccess(false);
		}
		return SUCCESS;
	}
	
	public String updateByPrimaryKeySelective(){
		List<StudentScoresPojo> studentScoreList = JSON.parseArray(studentScoreListStr, StudentScoresPojo.class);
		try {
			for(int i=0;i<studentScoreList.size();i++){
				StudentScoresPojo value = studentScoreList.get(i);
				
				//更新
				
					sv.updateByPrimaryKeySelective(value);
				
			}
			this.setSuccess(true);
		} catch (Exception e) {
			this.setSuccess(false);
			e.printStackTrace();
		}
		return SUCCESS;
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getStudentId() {
		return studentId;
	}
	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}
	public String getStudentName() {
		return StudentName;
	}
	public void setStudentName(String studentName) {
		StudentName = studentName;
	}
	public String getCourseName() {
		return courseName;
	}
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	public String getCredit() {
		return credit;
	}
	public void setCredit(String credit) {
		this.credit = credit;
	}
	public String getTestScores() {
		return testScores;
	}
	public void setTestScores(String testScores) {
		this.testScores = testScores;
	}
	public String getPeacetimeScores() {
		return peacetimeScores;
	}
	public void setPeacetimeScores(String peacetimeScores) {
		this.peacetimeScores = peacetimeScores;
	}
	public String getProm() {
		return prom;
	}
	public void setProm(String prom) {
		this.prom = prom;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public String getHourse() {
		return hourse;
	}
	public void setHourse(String hourse) {
		this.hourse = hourse;
	}
	public String getIsAgain() {
		return isAgain;
	}
	public void setIsAgain(String isAgain) {
		this.isAgain = isAgain;
	}
	public String getScores() {
		return scores;
	}
	public void setScores(String scores) {
		this.scores = scores;
	}
	public Boolean getSuccess() {
		return success;
	}
	public void setSuccess(Boolean success) {
		this.success = success;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public String getLimit() {
		return limit;
	}
	public void setLimit(String limit) {
		this.limit = limit;
	}
	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	public PageInfo<StudentScoresPojo> getResultList() {
		return resultList;
	}
	public void setResultList(PageInfo<StudentScoresPojo> resultList) {
		this.resultList = resultList;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getStudentScoreListStr() {
		return studentScoreListStr;
	}

	public void setStudentScoreListStr(String studentScoreListStr) {
		this.studentScoreListStr = studentScoreListStr;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	
	
	
	
	
	

}
